#!/bin/bash
set -euo pipefail

. ${KOLA_EXT_DATA}/libtest.sh
cd $(mktemp -d)

# From https://github.com/ostreedev/ostree/blob/95a6d1514/tests/kolainst/destructive/overlay-initrds.sh#L23
check_for_dracut_karg() {
  local karg=$1; shift
  # https://github.com/dracutdevs/dracut/blob/38ea7e821b/modules.d/98dracut-systemd/dracut-cmdline.sh#L17
  journalctl -b 0 -t dracut-cmdline \
    --grep "Using kernel command line parameters:.* ${karg} "
}

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
  "")
    for f in / /var /usr/bin; do
      if rpm-ostree ex initramfs-etc --track ${f} 2>out.txt; then
        fatal "should have failed with path ${f}"
      fi
      assert_file_has_content_literal out.txt "Path outside /etc forbidden: ${f}"
      rm -f out.txt
    done

    mkdir -p /etc/cmdline.d
    echo 'foobar' > /etc/cmdline.d/foobar.conf

    rpm-ostree ex initramfs-etc --track /etc/cmdline.d/foobar.conf
    rpm-ostree status > status.txt
    assert_file_has_content_literal status.txt "InitramfsEtc: /etc/cmdline.d/foobar.conf"
    rpm-ostree status --json > status.json
    assert_jq status.json \
      '.deployments[0]["initramfs-etc"]|length == 1' \
      '.deployments[0]["initramfs-etc"][0] == "/etc/cmdline.d/foobar.conf"'

    /tmp/autopkgtest-reboot 1
    ;;
  1)
    check_for_dracut_karg foobar
    rpm-ostree ex initramfs-etc --track /etc/cmdline.d/foobar.conf > out.txt
    assert_file_has_content_literal out.txt "No changes."

    # right now we don't rechecksum all the files so changing the file alone
    # isn't noticed, but we could in the future
    echo 'barbaz' > /etc/cmdline.d/foobar.conf
    rpm-ostree ex initramfs-etc --track /etc/cmdline.d/foobar.conf > out.txt
    assert_file_has_content_literal out.txt "No changes."

    # but --force-sync should also plow through
    rpm-ostree ex initramfs-etc --force-sync > out.txt
    assert_file_has_content_literal out.txt "Staging deployment"

    # test that we can created parent dirs and we don't include other files in
    # that hierarchy
    mkdir -p /etc/foo/bar/baz/boo
    echo 'supernested' > /etc/foo/bar/baz/boo/nested.conf
    touch /etc/foo/bar/this_better_not_be_included
    mkdir /etc/foo/bar/this
    touch /etc/foo/bar/this/neither
    ln -sf /etc/foo/bar/baz/boo/nested.conf /etc/cmdline.d/
    rpm-ostree ex initramfs-etc --track /etc/cmdline.d/nested.conf --track /etc/foo/bar/baz/boo

    /tmp/autopkgtest-reboot 2
    ;;
  2)
    check_for_dracut_karg barbaz
    check_for_dracut_karg supernested
    if check_for_dracut_karg foobar; then
      assert_not_reached "Found karg foobar; expected barbaz"
    fi

    latest_overlay=$(ls -t /boot/ostree/initramfs-overlays | head -n1)
    lsinitrd "/boot/ostree/initramfs-overlays/${latest_overlay}" > out.txt
    assert_not_file_has_content_literal out.txt this_better_not_be_included neither

    # let's try tracking a whole directory instead
    echo 'bazboo' > /etc/cmdline.d/bazboo.conf
    # and for fun, let's use the the locked finalization flow
    rpm-ostree ex initramfs-etc --lock-finalization \
      --untrack /etc/cmdline.d/foobar.conf \
      --untrack /etc/cmdline.d/nested.conf \
      --track /etc/cmdline.d
    rpm-ostree status > status.txt
    assert_file_has_content_literal status.txt "InitramfsEtc: /etc/cmdline.d"
    rpm-ostree status --json > status.json
    assert_jq status.json \
      '.deployments[0]["initramfs-etc"]|length == 2' \
      '.deployments[0]["initramfs-etc"][0] == "/etc/foo/bar/baz/boo"' \
      '.deployments[0]["initramfs-etc"][1] == "/etc/cmdline.d"'

    /tmp/autopkgtest-reboot-prepare 3
    rpm-ostree finalize-deployment --allow-missing-checksum
    ;;
  3)
    check_for_dracut_karg barbaz
    check_for_dracut_karg bazboo
    check_for_dracut_karg supernested

    # finally, check that passing no args prints the tracked files
    rpm-ostree ex initramfs-etc > out.txt
    assert_file_has_content_literal out.txt "Tracked files:"
    assert_file_has_content_literal out.txt "/etc/cmdline.d"
    ;;
  *) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac
